import requests
import json
import time
import configparser

def coin_info(ext_symbol = ''):
    config = configparser.ConfigParser()
    # -read读取ini文件
    config.read('config.ini', encoding='GB18030')
    default_url = config.get("coin_info", "default_url")
    headers = {
        'Content-Type': 'application/json',
        'cache-control': 'no-cache',
    }
    info = ""
    while 1:
        try:
            url = default_url
            res = requests.request("POST", url, headers=headers)
            coin_info = json.loads(res.text)
            coin_info = coin_info['data']['list']
            default_symbol = ["BTC", "ETH", "BCH"]
            default_symbol.append(ext_symbol)
            for item in coin_info:
                if item['symbol'] in default_symbol:
                    info += (item['symbol'] + ' ' +str(round(float(item['price']), 2)) + ' ' + str(item['percent_change_utc8']) + '%\n')
            #print(info)
            return info
        except:
            assert(0)
            time.sleep(600)
            #url = get_url()
            #default_url = url
            print("Net broken! Retry!")

coin_info()
